package com.compass.doctor.ui.home.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.R
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.callback.OnDrugSelectClick
import com.compass.doctor.databinding.FragmentDrugsBinding
import com.compass.doctor.dialog.AppendDrugDialog
import com.compass.doctor.ui.home.adapter.DrugItemAdapter
import com.compass.framework.constant.KEY_CONSULT_TYPE
import com.compass.framework.constant.KEY_LIST
import com.compass.framework.dialog.CommonMessageDialog
import com.compass.framework.ext.gone
import com.compass.framework.ext.toBeanOrNull
import com.compass.framework.ext.visible
import com.compass.framework.model.DrugItem
import com.compass.framework.utils.getStringFromResource

/**
 * Description： 药品Fragment
 * Date:2023/12/7 10:35
 */
class DrugFragment : BaseVBFragment<FragmentDrugsBinding>() {
    //1全部药品 2模板 3是处方开药
    private var  intType:Int?=null
    private lateinit var mAdapter: DrugItemAdapter
    private lateinit var onDrugItemClick:OnDrugSelectClick
    companion object {
        fun newInstance(jsonStr: String, intType: Int): DrugFragment {
            val fragment = DrugFragment()
            val bundle = Bundle()
            bundle.putString(KEY_LIST, jsonStr)
            bundle.putInt(KEY_CONSULT_TYPE, intType)
            fragment.arguments = bundle
            return fragment
        }
    }

     fun setList( drugItems : MutableList<DrugItem>?){
        mAdapter.setData(drugItems)
         if (drugItems.isNullOrEmpty()) {
             mBinding?.emptyView?.visible()
         } else {
             mBinding?.emptyView?.gone()
         }
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        //1全部药品 2模板 3是处方开药
        intType= arguments?.getInt(KEY_CONSULT_TYPE,1)
        intType?.let {
            mAdapter = DrugItemAdapter(it)
        }
        mBinding?.recyclerView?.apply {
            layoutManager =
                LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
            adapter = mAdapter
        }
        mAdapter.onItemClickListener = { _: View, position: Int ->
            val item = mAdapter.getItem(position)
            //待定没有药品详情

        }
        mAdapter.setBottomCallBack(onBottomClick)
    }

    override fun initData() {
        val listJson = arguments?.getString(KEY_LIST, "")
        val list = listJson?.toBeanOrNull<MutableList<DrugItem>>()
        mAdapter.setData(list)
        if (list.isNullOrEmpty()) {
            mBinding?.emptyView?.visible()
        } else {
            mBinding?.emptyView?.gone()
        }
    }

     fun setDrugItemClick(onDrugItemClick:OnDrugSelectClick){
        this.onDrugItemClick=onDrugItemClick
    }


    private var onBottomClick = object : OnCustomButtonClick {

        override fun onButtonCallBack(position: Int, msgType: String?) {
            when (msgType) {
                "common_medicine" -> {
                    showCommonlyUsedDialog()
                }

                "prescription" -> {
                    val item = mAdapter.getItem(position)
                    if(::onDrugItemClick.isInitialized){
                        item?.let { AppendDrugDialog.Builder(requireActivity(),item,intType,onDrugItemClick).show() }
                    }
                }
            }
        }
    }

    /**
     * 添加常用药dialog
     */
    private fun showCommonlyUsedDialog() {
        CommonMessageDialog.Builder(requireActivity())
            .setTitle(getStringFromResource(com.compass.framework.R.string.dialog_tips_title))
            .setMessage(getStringFromResource(R.string.commonly_used_tips))
            .setConfirm(getStringFromResource(com.compass.framework.R.string.default_confirm))
            .setCancel(getString(com.compass.framework.R.string.default_cancel))
            .setonCancelListener {
                it?.dismiss()
            }
            .setonConfirmListener {
                showToast("添加常用药操作")
                it?.dismiss()
            }.create().show()
    }
}